var pageii = null;
var grid = null;
$(function() {

    grid = lyGrid({
        pagId : 'paging',
        l_column : [ {
            colkey : "deviceId",
            name : "设备编号",
        },{
            colkey : "deviceCode",
            name : "设备编码",
        },{
            colkey : "deviceName",
            name : "设备名称",
            isSort:true,
        }, {
            colkey : "deviceTypeName",
            name : "设备种类",
            isSort:true,
        },{
            colkey : "deviceStatus",
            name : "设备状态",
        }, {
            colkey : "devicePurchaseDate",
            name : "购买日期",
            renderData : function(rowindex,data, rowdata, column) {
                return new Date(data).format("yyyy-MM-dd hh:mm:ss");
            }
        }, {
            colkey : "devicePurchasePrice",
            name : "购买价格",
            isSort:true,
        }, {
            colkey : "deviceManufactureDate",
            name : "出厂日期",
            renderData : function(rowindex,data, rowdata, column) {
                return new Date(data).format("yyyy-MM-dd hh:mm:ss");
            }
        }, {
            colkey : "deviceServiceLife",
            name : "使用年限",
            isSort:true,
        }, {
            colkey : "deviceKeeper",
            name : "保管人"
        }
        ],
        jsonUrl : rootPath + '/device/deviceManagementPage.shtml',
        dymCol:true,
        checkbox : true,
        serNumber : false,
        checkValue: "deviceId",
        records: "list",
        totalPages: "pages",
        totalRecords: "total",
        pagecode: '10',
        pageNow : 'pageNum'
    });
    $("#search").click("click", function() {// 绑定查询按扭
        var searchParams = $("#deviceName").serializeJson();// 初始化传参数
        grid.setOptions({
            data : searchParams
        });
    });
    $("#replace").click("click", function() {// 绑定重置按扭
        $("#deviceName").val("");
    });
    $("#addAccount").click("click", function() {
        addAccount();
    });
    $("#editAccount").click("click", function() {
        editAccount();
    });
    $("#delAccount").click("click", function() {
        delAccount();
    });


});


function editAccount() {
    var cbox = grid.getSelectedCheckbox();
    if (cbox.length > 1 || cbox == "") {
        layer.msg("只能选中一个");
        return;
    }
    pageii = layer.open({
        title : "编辑设备",
        type : 2,
        area : [ "600px", "80%" ],
        content : rootPath + '/device/editDeviceUI.shtml?id=' + cbox
    });
}
function addAccount() {
    pageii = layer.open({
        title : "新增设备",
        type : 2,
        area : [ "600px", "80%" ],
        content : rootPath + '/device/addDeviceUI.shtml'
    });
}
function delAccount() {
    var cbox = grid.getSelectedCheckbox();
    if (cbox == "") {
        layer.msg("请选择删除项！！");
        return;
    }
    layer.confirm('是否删除？', function(index) {
        var url = rootPath + '/device/deleteDevice.shtml';
        var s = CommnUtil.ajax(url, {
            ids : cbox.join(",")
        }, "json");
        if (s == "success") {
            layer.msg('删除成功');
            grid.loadData();
        } else {
            layer.msg('删除失败');
        }
    });
}
